//
//  YKQAddTypeViewController.m
//  YaoKongQi
//
//  Created by ryou on 2021/5/19.
//
#define kykqTitleArray @[@"空调",@"电视机",@"风扇"]

#import "YKQAddTypeViewController.h"
#import "YKQAddNameViewController.h"
@interface YKQAddTypeViewController ()<UITableViewDelegate,UITableViewDataSource,BUNativeExpressRewardedVideoAdDelegate>

@property (strong, nonatomic)UITableView *ykqTableView;
@property(nonatomic,assign)NSInteger ykqloadTag;

@end

@implementation YKQAddTypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.ykqloadTag=0;
    self.title = @"请选择遥控器设备";
    self.ykqTableView.delegate=self;
    self.ykqTableView.dataSource=self;
    self.ykqTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    self.ykqTableView.backgroundColor=kWhiteColor;
    [self.view addSubview: self.ykqTableView];
    
    [self ykqLoadRewardVideoAd];
    
    [self ykqBUAdLoadBanner];

    // Do any additional setup after loading the view from its nib.
}
-(UITableView *)ykqTableView{
    if (!_ykqTableView) {
        _ykqTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 100, kYKQWidth, kYKQHeight - 100) style:UITableViewStylePlain];
    }
    return _ykqTableView;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
  
    UITableViewCell *ykqCell=[tableView cellForRowAtIndexPath:indexPath];
    if (!ykqCell) {
        ykqCell=[[UITableViewCell alloc]init];
    }
    ykqCell.selectionStyle=UITableViewCellSelectionStyleNone;
    ykqCell.backgroundColor=kWhiteColor;
    
    UIImageView *bgimage=[[UIImageView alloc]initWithFrame:CGRectMake(20, 10, kYKQWidth  - 40, 80)];
    bgimage.image=[UIImage imageNamed:@"ykq_icon_white_bg.png"];
    bgimage.contentMode=UIViewContentModeScaleToFill;
    [ykqCell addSubview:bgimage];
    
    UILabel *nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(40, 0, 300, 80)];
    nameLabel.text =  kykqTitleArray[indexPath.row];
    nameLabel.textColor=kBlackColor;
    [bgimage addSubview:nameLabel];
    nameLabel.font=[UIFont systemFontOfSize:18];
 
    return ykqCell;
    
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return kykqTitleArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self ykqShowRewardVideoAd];
}



- (void)ykqLoadRewardVideoAd{
 
    BURewardedVideoModel *model = [[BURewardedVideoModel alloc] init];
    self.ykqReWardVideo = [[BUNativeExpressRewardedVideoAd alloc] initWithSlotID:kYkqBuadJili rewardedVideoModel:model];
    self.ykqReWardVideo.delegate = self;
    [self.ykqReWardVideo loadAdData];
    
}

- (void)ykqShowRewardVideoAd {
    if (self.ykqReWardVideo && self.ykqloadTag == 1 ) {
        [[YKQAlertView sharedInstance]showAlertWithWithTag:^{
          
            [self.ykqReWardVideo showAdFromRootViewController:self];
      
        }];

    }else if(self.ykqloadTag == 2){
        [self ykqShowaddViewController];
    }
}
-(void)ykqShowaddViewController{
    self.ykqloadTag = 0 ;
    YKQAddNameViewController *TYPE=[[YKQAddNameViewController alloc]init];
    TYPE.index=0;
    [self.navigationController pushViewController:TYPE animated:YES];
    [self  ykqLoadRewardVideoAd];
}

#pragma mark - BUNativeExpressRewardedVideoAdDelegate

- (void)nativeExpressRewardedVideoAd:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *_Nullable)error {
    self.ykqloadTag = 2;
}


- (void)nativeExpressRewardedVideoAdDidDownLoadVideo:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd {
    self.ykqloadTag = 1;
}
 
- (void)nativeExpressRewardedVideoAdDidPlayFinish:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd didFailWithError:(NSError *_Nullable)error {
    [self ykqShowaddViewController];
}
- (void)nativeExpressRewardedVideoAdDidClose:(BUNativeExpressRewardedVideoAd *)rewardedVideoAd{
    self.ykqloadTag = 0 ;
    [self  ykqLoadRewardVideoAd];
}
@end
